package com.jsonal.controller.api;


import com.jsonal.controller.BaseController;
import com.jsonal.entity.api.ArticleBean;
import com.jsonal.form.api.ArticleForm;
import com.jsonal.service.ArticleService;
import com.jsonal.service.UploadFilesService;
import com.jsonal.vo.Result;
import io.swagger.annotations.ApiOperation;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author auto
 * @since 2020-04-20
 */
@Controller
@RequestMapping("/api")
public class ArticleController extends BaseController {

    @Autowired
    private ArticleService articleService;

    @Autowired
    private UploadFilesService uploadFilesService;

    @RequestMapping(value = "/releaseBlog", method = RequestMethod.POST)
    @ApiOperation(value = "/releaseBlog", httpMethod = "POST", notes = "发布文章")
    @ResponseBody
    public Result releaseBlog(@RequestBody ArticleForm articleForm) {
        ArticleBean articleBean = new ArticleBean();
        this.setFormToBean3(articleForm, articleBean);
        return articleService.releaseBlog(articleBean);
    }

    @RequestMapping(value = "/getMyBlogList", method = RequestMethod.POST)
    @ApiOperation(value = "/getMyBlogList", httpMethod = "POST", notes = "获取某个用户的相关的所有博客文章")
    @ResponseBody
    public Result getMyBlogList(@RequestBody ArticleForm articleForm) {
        ArticleBean articleBean = new ArticleBean();
        this.setFormToBean3(articleForm, articleBean);
        return articleService.getMyBlogList(articleBean);
    }

    @RequestMapping(value = "/getBlogInfo/{id}", method = RequestMethod.POST)
    @ApiOperation(value = "/getBlogInfo/{id}", httpMethod = "POST", notes = "查看详细内容")
    @ResponseBody
    public Result getBlogInfo(@PathVariable String id) {
        if (id == null || "".equals(id)) {
            return new Result(false, "id不能为空");
        }
        return articleService.getBlogInfo(id);
    }

    @RequestMapping(value = "/updateInfo", method = RequestMethod.POST)
    @ApiOperation(value = "/updateInfo", httpMethod = "POST", notes = "更新数据")
    @ResponseBody
    public Result updateInfo(@RequestBody ArticleForm articleForm) {
        ArticleBean articleBean = new ArticleBean();
        this.setFormToBean3(articleForm, articleBean);
        return articleService.updateInfo(articleBean);
    }

    @RequestMapping(value = "/remove/{id}", method = RequestMethod.POST)
    @ApiOperation(value = "/remove/{id}", httpMethod = "POST", notes = "根据id 删除数据 （逻辑删除）")
    @ResponseBody
    public Result remove(@PathVariable String id) {
        if (id == null || "".equals(id)) {
            return new Result(false, "id不能为空");
        }
        return articleService.remove(id);
    }

    /**
     * 文件上传
     *
     * @param request req
     * @param file    file
     * @return result
     */
    @ResponseBody
    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    @ApiOperation(value = "/upload", httpMethod = "POST", notes = "文件上传至OSS服务器")
    public Result upload(HttpServletRequest request, @RequestParam(value = "file", required = false) MultipartFile file) {
        String folder = request.getParameter("folder");
        String fileId = request.getParameter("fileId");
        return uploadFilesService.saveFiles(file, folder, fileId);
    }

    @ResponseBody
    @RequestMapping(value = "getBlogList", method = RequestMethod.POST)
    @ApiOperation(value = "/getBlogList", httpMethod = "POST", notes = "首页获取数据")
    public Result getBlogList(ArticleBean articleBean) {
        return articleService.getBlogList(articleBean);
    }

    @RequestMapping(value = "/getHotList", method = RequestMethod.GET)
    @ApiOperation(value = "/getHotList", httpMethod = "GET", notes = "获取热门文章")
    @ResponseBody
    public Result getHotList() {
        return articleService.getHotList();
    }
}
